Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
C | find the size of the value a void Pointer is pointing at using sizeof()

Im trying to use sizeof() to find the size of the value a void pointer is pointing at, I want to use it to print the value.

this code

    int num = 5;
    void *voidPtr;
    voidPtr = #
    printf("%d", sizeof(*voidPtr));

Prints, 1. How can i get it to print 4?

If theres any other way to print the value of the void pointer, please let me know

Thanks in advance.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The value of pointer merely conveys the starting address of an object, not any information about its size.

The type of a pointer inherently conveys information about the size of the object it points to, if it is a complete type. However, void is incomplete, and a void * provides no information about the size of the object it originated from (except possibly by extensions to standard C and by debugging features).

Generally, if your program needs to know the size of what a void * points to, it must track that information itself.

over 4 years ago · Santiago Trujillo Relatório

0

  1. *voidPtr is invalid in standard C. It is a GCC extension and that is the reason why sizeof(*voidptr) is 1. Any other (non GCC family) compiler will not compile this code.

  2. %d is the wrong format to print size_t. You should use %zu instead.

  3. There is no way in C to determine what size object is referenced by the void pointer

over 4 years ago · Santiago Trujillo Relatório

0

The type void is an incomplete type. It means that the size of an expression of the type void is undefined.

From the C Standard (6.2.5 Types)

19 The void type comprises an empty set of values; it is an incomplete object type that cannot be completed

and (6.5.3.4 The sizeof and alignof operators)

1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, ...

Some compilers for back compatibility make sizeof( void ) equal to sizeof( char ) that is to 1.

If you need to get the size of the pointed object using a pointer of the type void * then you need to cast the pointer to the pointer of the referenced object type as for example

printf("%zu", sizeof( *( int * )voidPtr));

Pay attention to that you need to use the conversion specifier %zu with expressions of the type size_t.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda